package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjSettlement;
import com.koron.css2.newProjectCenter.bean.query.PjSettlementQuery;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjSettlementMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 工程报装-编制结算删除
 */
public class PjSettlemetDelete implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjSettlementQuery pjSettlementQuery = JsonUtils.objectToPojo(req.getData(),PjSettlementQuery.class);
            PjSettlementMapper pjSettlementMapper = factory.getMapper(PjSettlementMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            if(StringUtils.isEmpty(pjSettlementQuery.getId())) {
            	return MessageBean.create(Constant.MESSAGE_INT_FAIL,"id 不能为空",null);
            }
            PjSettlement pjSettlement = pjSettlementMapper.getSingle(pjSettlementQuery);
            pjSettlementMapper.deleteById(pjSettlementQuery.getId());
            if (pjSettlement != null){
                pjInfoMapper.updateSettlementFlag(pjSettlement.getConsultId(), 0);
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "删除成功", List.class);
        } catch (Exception e) {
            logger.error("编制结算删除失败",e);
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "编制结算删除失败", null);
    }
}
